package org.ztuffaha.amqsecurity;
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.io.File;

import org.apache.activemq.broker.Broker;
import org.apache.activemq.broker.BrokerFilter;
import org.apache.activemq.broker.ConnectionContext;
import org.apache.activemq.broker.region.Subscription;
import org.apache.activemq.command.ConnectionInfo;
import org.apache.activemq.command.ProducerInfo;
import org.apache.activemq.command.ConsumerInfo;
import org.apache.activemq.security.SecurityContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Handles authenticating a users against a simple user name/password 
 * map.
 *
 * @version $Revision$
 */
public class ZSecurityBroker extends BrokerFilter
{

    AuthenticationMap userMap = new AuthenticationMap();
    private static final Log LOG = LogFactory.getLog(ZSecurityBroker.class);

    public ZSecurityBroker(Broker next, String fileName, int scanTimeInSeconds)
    {
        super(next);
        new Thread(new Reloader(userMap, fileName, scanTimeInSeconds)).start();
    }

    //methods like: createQueueSession will invoke addConnection and addConsumer
    //addConsumer is invoked because createQueueSession receives acknowledgements !

    public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception
    {
        //permissions are:
        //add, read, write
        List userList = (List) userMap.getAllUsres();
        String password = info.getPassword();
        String userName = info.getUserName();
        if (userMap.isValidUser(userName, password))
        {
            super.addConnection(context, info);
        }
        else
        {
            throw new SecurityException("User " + info.getUserName() + " is not authorized to add a connection ");
        }
    }

    public void removeConnection(ConnectionContext context, ConnectionInfo info, Throwable error)
            throws Exception
    {
        super.removeConnection(context, info, error);
    }

    public Subscription addConsumer(ConnectionContext context, ConsumerInfo info) throws Exception
    {
        if (userMap.hasReadAccess(context.getUserName(), info.getDestination().getPhysicalName()))
        {
            return super.addConsumer(context, info);
        }
        else
        {
            throw new SecurityException("User " + context.getUserName() + " is not authorized to write ");
        }
    }

    public void addProducer(ConnectionContext context, ProducerInfo info) throws Exception
    {
        if (userMap.hasWriteAccess(context.getUserName(), info.getDestination().getPhysicalName()))
        {
            super.addProducer(context, info);
        }
        else
        {
            throw new SecurityException("User " + context.getUserName() + " is not authorized to write ");
        }
    }
}
